package cn.tiasen.helloworld;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Page1Activity extends AppCompatActivity {

    private EditText et_1;
    private Button btn_1;
    private Button btn_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page1);

        et_1 = findViewById(R.id.et_1);
        btn_1 = findViewById(R.id.btn_1);
        btn_2 = findViewById(R.id.btn_2);

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Page1Activity.this, Page2Activity.class);
                startActivity(intent);
            }
        });

        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String et_value = et_1.getText().toString();
                if (TextUtils.isEmpty(et_value)) {
                    Toast.makeText(Page1Activity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(Page1Activity.this, Page2Activity.class);
                    intent.putExtra("params", et_value);
                    startActivityForResult(intent,2);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(2 == requestCode && 3 == resultCode){
            String result = data.getStringExtra("RESULT");
            et_1.setText(result);
        }
    }
}
